package com.yunxi.service.dubboService.impl.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yunxi.core.util.BeanTools;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.model.sys.Area;
import com.yunxi.service.localService.sys.AreaService;
import com.yunxi.sys.pojo.AreaVO;
import com.yunxi.sys.service.AreaDubboService;
import com.yunxi.util.RedisCacheService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 地域信息 服务实现类
 * </p>
 *
 * @author
 * @since 2019-05-07
 */
public class AreaDubboServiceImpl implements AreaDubboService {

   @Autowired
   @Qualifier(value = "areaServiceImpl")
   AreaService areaService;

   @Autowired
   RedisCacheService redisCacheService;

   @Override
   public AreaVO getById(String id) throws InvocationTargetException, IllegalAccessException {
      Area area = areaService.getById(id);
      AreaVO areaVO = new AreaVO();
      BeanUtils.copyProperties(area, areaVO);
      return areaVO;
   }

   @Override
   public void saveOrUpdate(AreaVO areaVO) throws InvocationTargetException, IllegalAccessException {
      if (BeanTools.isAllFieldNull(areaVO)) return;
      Area area = new Area();
      BeanUtils.copyProperties(areaVO, area);
      areaService.saveOrUpdate(area);

      redisCacheService.delete("area::area_type" + areaVO.getType());
   }

   @Override
   public void removeById(String id) {
      QueryWrapper<Area> wrapper = new QueryWrapper<>();
      wrapper.eq("AREA_ID", id);
      Area one = areaService.getOne(wrapper);
      areaService.removeById(id);
      redisCacheService.delete("area::area_type" + one.getType());
   }

   @Override
   public PageInfo<AreaVO> findPageByVO(AreaVO areaVo, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {
      PageInfo<AreaVO> areaList = areaService.findPageNotExit(areaVo, pageNum, pageSize);
      return areaList;
   }

   @Override
   public PageInfo<AreaVO> pageByVO(AreaVO areaVo, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {
      PageInfo<AreaVO> areaList = areaService.pageByVo(areaVo, pageNum, pageSize);
      return areaList;
   }

   @Override
   public List<AreaVO> listByPid(String selectvalues) {
      return areaService.listByPid(selectvalues);
   }

   @Override
   public Object treeByPid() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
      return areaService.treeByPid();
   }

   @Override
   public List<AreaVO> listProvince() {
      return areaService.listProvince();
   }

   @Override
   public Object treeByType(Integer type) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
      return areaService.treeByType(type);
   }

   @Override
   public void saveBatch(AreaVO areaVO) throws Exception {
      areaService.saveBatchCas(areaVO);
   }

   @Override
   public Object listByVo(AreaVO v, LoginInfo loginInfo) {
      return areaService.listByVo(v);
   }

   @Override
   public Object loadCas(AreaVO areaVO) {
      return areaService.loadCas(areaVO);
   }

   @Override
   public void delCas(List<AreaVO> areaVOList) {
      areaService.delCas(areaVOList);
   }

   @Override
   public ApiResult listAllProvince(AreaVO areaVO, LoginInfo loginInfo) {
      return areaService.listAllProvince(areaVO,  loginInfo);
   }

   @Override
   public AreaVO getByProvinceCode(String provinceCode) {
      AreaVO areaVO = new AreaVO();
      areaVO.setType(1);
      areaVO.setCode(provinceCode);
      List<AreaVO> list = areaService.listByVo(areaVO);
      if (list != null && list.size() > 0) {
         areaVO = list.get(0);
         AreaVO vo = new AreaVO();
         vo.setCode(areaVO.getPcode());
         vo.setType(1);
         List<AreaVO> listVO = areaService.listByVo(vo);
         if (listVO != null && listVO.size() > 0) {
            areaVO = listVO.get(0);
         }
      }
      return areaVO;
   }

   @Override
   public String getBigRegionByCityCode(String cityCode) {
     return areaService.getBigRegionByCityCode(cityCode);
   }

   @Override
   public AreaVO queryAreaByPartnerCode(String partnerCode){
      return areaService.queryAreaByPartnerCode(partnerCode);
   }
}
